package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.constant.CommonConstants;
import com.rm.zeyou.entity.Broker;
import com.rm.zeyou.entity.BrokerConsumeDetail;
import com.rm.zeyou.entity.Technician;
import com.rm.zeyou.mapper.BrokerConsumeDetailMapper;
import com.rm.zeyou.mapper.BrokerMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.BrokerConsumeDetailService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.request.BrokerIncomeVo;
import com.rm.zeyou.vo.response.BrokerIncomeResponseBaseVo;
import com.rm.zeyou.vo.response.BrokerIncomeResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 经纪人佣金记录表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:07
 */
@Service
public class BrokerConsumeDetailServiceImpl extends ServiceImpl<BrokerConsumeDetailMapper, BrokerConsumeDetail> implements BrokerConsumeDetailService {
    @Resource
    private BrokerConsumeDetailMapper brokerConsumeDetailMapper;
    @Resource
    private BrokerMapper brokerMapper;

    @Override
    public ApiRestResponse<BrokerIncomeResponseBaseVo> brokerIncome(Technician technician, BrokerIncomeVo requestVo) {
        Broker broker = brokerMapper.selectOne(new LambdaQueryWrapper<Broker>()
                .eq(Broker::getTid, technician.getId()));
        BrokerIncomeResponseBaseVo baseVo = new BrokerIncomeResponseBaseVo();
        List<BrokerIncomeResponseVo> voList = new ArrayList<>();
        BigDecimal addAmount = brokerConsumeDetailMapper.selectAddAmount(broker.getId());
        baseVo.setTotalIncome(addAmount);
        PageHelper.startPage(requestVo.getPageNum(), requestVo.getPageSize());
        List<BrokerConsumeDetail> brokerConsumeDetails = brokerConsumeDetailMapper.selectList(new LambdaQueryWrapper<BrokerConsumeDetail>()
                .eq(BrokerConsumeDetail::getBrokerId, broker.getId())
                .eq(BrokerConsumeDetail::getConsumptionType, CommonConstants.CONSUMPTION_ADD)
                .orderByDesc(BrokerConsumeDetail::getCreateTime));
        if (!CheckEmptyUtil.isEmpty(brokerConsumeDetails)) {
            PageInfo<BrokerConsumeDetail> source = new PageInfo<>(brokerConsumeDetails);
            PageInfo<BrokerIncomeResponseVo> target = new PageInfo<>();
            BeanUtils.copyProperties(source, target);
            source.getList().forEach(item -> {
                BrokerIncomeResponseVo vo = new BrokerIncomeResponseVo();
                vo.setDescribe("佣金");
                vo.setCreateTime(item.getCreateTime());
                voList.add(vo);
            });
            target.setList(voList);
            baseVo.setBrokerIncomeList(target);
        } else {
            baseVo.setBrokerIncomeList(new PageInfo<>());
        }


        return ApiRestResponse.success(baseVo);
    }
}
